<?php
require_once FOREGROUND_COMMON_ACTION_SERVICE_DIR . '/ajax.php';

class foreground_order_add_action_service extends foreground_ajax_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {	     		     
	    //var_dump(__METHOD__); exit();
        //var_dump($_SESSION); exit();
    
        $result = array('status' => 0, 'message' => '', 'data' => array());
    
        //var_dump($_POST); exit();
        $address_id = init_user_data_key($_POST, 'address_id', 'int');                          
    
        $action_data = wwy_action::$data;
	    $session_member_info = $action_data['session_member_info'];
        $member_id = $session_member_info['id'];                 
            
        if(empty($address_id)){
            $result['status']  = -2;
            $result['message'] = '请选择收货地址';
        }else{
            $model_foreground_member_address = wwy_resource_container::get_model_foreground_member_address();
            //var_dump($model_foreground_member_address); exit();            
            $where      = array(
                'id'        => $address_id,
                'member_id' => $member_id,
            );
            $address_row = $model_foreground_member_address->fetch_row($where);
            //var_dump($address_row); exit();
    
            if(empty($address_row)){
                $result['status']  = -3;
                $result['message'] = '提交订单失败';
            }
        }
        
        if($result['status'] == 0){
            $model_foreground_cart = wwy_resource_container::get_model_foreground_cart();
            //var_dump($model_foreground_cart); exit();
            $cart_list_info = $model_foreground_cart->get_cart_list_info($member_id);
            //var_dump($cart_list_info); exit();
            if(empty($cart_list_info['cart_list'])){
                $result['status']  = -4;
                $result['message'] = '购物车没有商品';
            }elseif($cart_list_info['total'] < self::$data['business_config']['order_min']){
                $result['status']  = -5;
                $result['message'] = '订单请满' . self::$data['business_config']['order_min'] . '元 ^_^';
            }
        }
        
        if($result['status'] == 0){                                  
            $model_foreground_order = wwy_resource_container::get_model_foreground_order();
            //var_dump($model_foreground_order); exit();
            $order_info = array(
                'cart_list_info' => $cart_list_info,
                'member_info'    => array('member_id' => $member_id),
                'shipping_info'  => array('address_info' => $address_row) ,
            );
            $add_order_result = $model_foreground_order->add_order($order_info);
            //var_dump($add_order_result); exit();
        
            if($add_order_result['status'] < 0){
                $result['status']  = -6;
                $result['message'] = '提交订单失败';
            }else{
                $result['status']  = 1;
                $result['message'] = '提交订单成功';
                //$result['data']['order_id'] = $add_order_result['data']['order_id'];   
                self::add_view_data('order_id', $add_order_result['data']['order_id']);
            }
        }
         
        self::set_view_status($result['status']);
        self::set_view_message($result['message']);
    }
}